package org.study.redis.api.util.jedis;

import redis.clients.jedis.Jedis;

import java.util.List;

/**
 * Redis 操作工具类：HyperLogLog
 * 参考 {@link redis.clients.jedis.commands.HyperLogLogCommands}
 * <p>
 * PFADD key element [element ...]：添加指定元素到 HyperLogLog 中。
 * PFCOUNT key [key ...]：返回给定 HyperLogLog 的基数估算值。
 * PFMERGE destkey sourcekey [sourcekey ...]：将多个 HyperLogLog 合并为一个 HyperLogLog
 *
 * @author doveylovey
 * @email 1135782208@qq.com
 * @date 2022年04月23日
 */
public class RedisHyperLogLogUtils {
    private RedisHyperLogLogUtils() {
        throw new AssertionError("不能实例化 RedisHyperLogLogUtils 工具类");
    }

    /**
     * 添加指定元素到 HyperLogLog 中
     *
     * @param key
     * @param elements
     * @return long
     */
    public static long pfadd(String key, String... elements) {
        long pfadd;
        try (Jedis jedis = SysJedisPool.getResource()) {
            pfadd = jedis.pfadd(key, elements);
        }
        return pfadd;
    }

    public static long pfadd(String key, List<String> elements) {
        long pfadd;
        try (Jedis jedis = SysJedisPool.getResource()) {
            pfadd = jedis.pfadd(key, elements.toArray(new String[elements.size()]));
        }
        return pfadd;
    }

    /**
     * 将多个 HyperLogLog 合并为一个 HyperLogLog
     *
     * @param destkey
     * @param sourcekeys
     * @return java.lang.String
     */
    public static String pfmerge(String destkey, String... sourcekeys) {
        String pfmerge = null;
        try (Jedis jedis = SysJedisPool.getResource()) {
            pfmerge = jedis.pfmerge(destkey, sourcekeys);
        }
        return pfmerge;
    }

    public static String pfmerge(String destkey, List<String> sourcekeys) {
        String pfmerge = null;
        try (Jedis jedis = SysJedisPool.getResource()) {
            pfmerge = jedis.pfmerge(destkey, sourcekeys.toArray(new String[sourcekeys.size()]));
        }
        return pfmerge;
    }

    /**
     * 返回给定 HyperLogLog 的基数估算值
     *
     * @param key
     * @return long
     */
    public static long pfcount(String key) {
        long pfcount;
        try (Jedis jedis = SysJedisPool.getResource()) {
            pfcount = jedis.pfcount(key);
        }
        return pfcount;
    }

    public static long pfcount(String... keys) {
        long pfcount;
        try (Jedis jedis = SysJedisPool.getResource()) {
            pfcount = jedis.pfcount(keys);
        }
        return pfcount;
    }

    public static long pfcount(List<String> keys) {
        long pfcount;
        try (Jedis jedis = SysJedisPool.getResource()) {
            pfcount = jedis.pfcount(keys.toArray(new String[keys.size()]));
        }
        return pfcount;
    }

    /*public static void main(String[] args) {
        System.out.println("pfadd：" + pfadd("relation", "MySQL", "Oracle", "SQL Server"));
        System.out.println("pfadd：" + pfadd("nosql", Lists.newArrayList("Redis", "MongoDB")));

        System.out.println("pfmerge：" + pfmerge("databases", "relation", "nosql"));
        System.out.println("pfmerge：" + pfmerge("databases", Lists.newArrayList("relation", "nosql")));

        System.out.println("pfcount：" + pfcount("databases"));
        System.out.println("pfcount：" + pfcount("databases", "relation", "nosql"));
        System.out.println("pfcount：" + pfcount(Lists.newArrayList("relation", "nosql")));
    }*/
}
